package com.lnsf.bookstore.web.borrow.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.lnsf.bookstore.common.annotation.IgnoreAuth;
import com.lnsf.bookstore.common.annotation.LoginUser;
import com.lnsf.bookstore.common.annotation.SysLog;
import com.lnsf.bookstore.common.controller.ApiBaseAction;
import com.lnsf.bookstore.common.dto.ResultFailDto;
import com.lnsf.bookstore.common.dto.ResultSuccessDto;
import com.lnsf.bookstore.web.book.service.ApiBookService;
import com.lnsf.bookstore.web.book.vo.ApiBook;
import com.lnsf.bookstore.web.borrow.service.ApiBorrowService;
import com.lnsf.bookstore.web.borrow.vo.ApiBorrow;
import com.lnsf.bookstore.web.user.vo.ApiUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/borrow")
public class ApiBorrowController extends ApiBaseAction{

    @Autowired
    private ApiBookService apiBookService;
    @Autowired
    private ApiBorrowService apiBorrowService;



    @RequestMapping("/list")
    public Object list(@LoginUser ApiUser apiUser,Integer status){
        Map<String,Object> resultObject = new HashMap<String, Object>();
        Map param = new HashMap();
        param.put("userId",apiUser.getUserId());
        param.put("status",status);
        List<ApiBorrow> apiBorrowList = apiBorrowService.queryListByUid(param);
        resultObject.put("borrowList",apiBorrowList);
        return toResponsSuccess(resultObject);
    }

    @RequestMapping("/remove")
    public Object remove(@LoginUser ApiUser apiUser){
        JSONObject jsonObject = getJsonRequest();
        Map<String,Object> resultObject = new HashMap<String, Object>();
        ApiBorrow apiBorrow =jsonObject.getObject("borrow",ApiBorrow.class);
        Map param = new HashMap();
        param.put("userId",apiUser.getUserId());
        param.put("id",apiBorrow.getId());
        apiBorrowService.delete(param);
        return toResponsSuccess(list(apiUser,0));
    }

    @RequestMapping("/getBorrow")
    @IgnoreAuth
    public Object getBorrow(@LoginUser ApiUser apiUser,Integer borrowId){
        JSONObject jsonObject = getJsonRequest();
        Map<String,Object> resultObject = new HashMap<String, Object>();
        ApiBorrow apiBorrow = apiBorrowService.queryObject(borrowId);
        resultObject.put("borrow",apiBorrow);
        return toResponsSuccess(resultObject);
    }

    /**
     * 修改状态
     * @param borrowId
     */
    @ResponseBody
    @RequestMapping(value="updateStatus")
    public int updateStatus(@LoginUser ApiUser apiUser,Integer borrowId,Integer status) {
        logger.info("borrowId:"+borrowId);
        ApiBorrow apiBorrow = new ApiBorrow();
        apiBorrow.setStatus(status);
        apiBorrow.setId(borrowId);
        return apiBorrowService.updateStatus(apiBorrow);
    }

}
